# -*- coding:utf-8 -*-
import re
from flask import jsonify


# 验证手机号是否正确
def phone_validatte(phone):
    # phone_pat = re.compile('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$')
    phone_pat = re.compile('^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$')
    res = re.search(phone_pat, phone)
    if res:
        return True
    else:
        return False


#
class checkPasswd(object):
    def checklen(self, pwd):
        return 8 <= len(pwd) <= 16

    def checkContainUpper(self, pwd):
        pattern = re.compile('[A-Z]+')
        match = pattern.findall(pwd)
        if match:
            return True
        else:
            return False

    def checkContainNum(self, pwd):
        pattern = re.compile('[0-9]+')
        match = pattern.findall(pwd)
        if match:
            return True
        else:
            return False

    def checkContainLower(self, pwd):
        pattern = re.compile('[a-z]+')
        match = pattern.findall(pwd)
        if match:
            return True
        else:
            return False

    def checkSymbol(self, pwd):
        pattern = re.compile('([^a-z0-9A-Z])+')
        match = pattern.findall(pwd)
        if match:
            return True
        else:
            return False

    def check(self, pwd):
        # 密码长度在8-16位
        if self.checklen(pwd):
            if self.checkContainUpper(pwd) and self.checkContainLower(pwd) and self.checkContainNum(
                    pwd) and self.checkSymbol(pwd):
                return True, '密码强度高'
            elif self.checkContainUpper(pwd) and self.checkContainLower(pwd) and self.checkContainNum(
                    pwd) and not self.checkSymbol(pwd):
                return True, '密码强度中等'
            elif not self.checkContainUpper(pwd) and self.checkContainLower(pwd) and self.checkContainNum(
                    pwd) and self.checkSymbol(pwd):
                return True, '密码强度中等'
            elif self.checkContainUpper(pwd) and not self.checkContainLower(pwd) and self.checkContainNum(
                    pwd) and self.checkSymbol(pwd):
                return True, '密码强度中等'
            elif self.checkContainUpper(pwd) and self.checkContainLower(pwd) and not self.checkContainNum(
                    pwd) and self.checkSymbol(pwd):
                return True, '密码强度中等'
            else:
                return True, '密码强度太弱'
        else:
            return False, '密码长度必须在8-16位'
